package com.sitech.ibnms.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import bsh.Interpreter;

public class InterpreterUtil {

	public static Boolean eval(String logicStatement,Map<String,Object> map) throws Exception{
		Interpreter  interpreter = new Interpreter();		
		
		Iterator iter = map.entrySet().iterator();

		while (iter.hasNext()) {

			Map.Entry entry = (Map.Entry) iter.next(); 
			Object key = entry.getKey();
			Object val = entry.getValue();

			System.out.println("key="+key);
			System.out.println("val="+val);
//			interpreter.set(key.toString(), val);
			logicStatement = logicStatement.replaceAll(key.toString(), val.toString());
		}
		
		System.out.println("logicStatement="+logicStatement);
		
		logicStatement="result=("+logicStatement+")";
		interpreter.eval(logicStatement);
		Boolean b = (Boolean)interpreter.get("result");
		
		System.out.println("result="+b);
		
		return b;
	}
	
	
	
	public static void main(String[] avg){
		String str = "KPI_VALUE>=2";
		String val = "2";
		
		Map map = new HashMap();
		map.put("KPI_VALUE", val);
		
		try{
			
			Boolean b = InterpreterUtil.eval(str, map);
			
			System.out.println("result="+b);
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
